home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 January / macformat-020.iso / Shareware City / Developers / AIFF_DSP_v15 folder / AIFF_DSP / interface.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-01  |  5.3 KB  |  162 lines  |  [TEXT/KAHL]

  1. /*
  2. FILE:     interface.c
  3. PROJECT:  Ford frant DSP
  4. AUTHOR:   Ben Denckla
  5.  
  6. COMMENTS:
  7.  
  8.     Contains functions which allow file selection via toolbox routines
  9.     in a non-toolbox-based program.
  10.  
  11.     PathNameFrom* functions were adapted from sample code in Symantec's
  12.     Think Reference.  Symantec got this code from an Apple Q&A Stack.
  13.     Some of the comments in these functions are speculative in that they
  14.     are my attempt to explain Apple's code.  I have not taken the
  15.     time to verify that my suppositions are correct.
  16.  
  17.     SF???File() reference was p. 367-373 of:
  18.     Huxham, Fred A., David Burnard, and Jim Takatsuka.  _Using the Macintosh
  19.         Toolbox With C_, 2nd ed.  San Francisco: Sybex, 1989. 
  20.  
  21.     Assumes #inclusion of MacHeaders and linkage of MacTraps library
  22. */
  23.  
  24. #include <string.h>
  25. #include <stdio.h>
  26. #include "interface.h"
  27.  
  28. int PathNameFromWD    (             long vRefNum, char* pathName );
  29. int PathNameFromDirID (long dirID, short vRefNum, char* fullPathName);
  30.  
  31. /*
  32. getfullfstr() gets a full input file name from a dialog box selection. (1)
  33. It displays files of all "numtyp" types specified in "types".
  34. If numtyp == -1, files of all types are displayed.
  35. */
  36. int getfullfstr( Point where, int numtyp, SFTypeList types, char *fstr ) {
  37.     SFReply reply;
  38.     int err;
  39.  
  40.     printf("\n"); // 2
  41.     SFGetFile( where, "\p" , NULL, numtyp, types, NULL, &reply);
  42.  
  43.     if (!reply.good) return 1; // 3
  44.  
  45.     err = PathNameFromWD( reply.vRefNum, fstr ); // 4
  46.     if (err) return 1;
  47.     strcat( fstr, PtoCstr( reply.fName ) ); // 5
  48.     printf( "Input File Chosen:\n%s\n", fstr );
  49.     return 0;
  50. }
  51.  
  52. /*
  53. putfullfstr() gets a full output file name from a dialog box selection. (1)
  54. It displays prompt "prompt" and default file name "fstr" in the dialog box.
  55. */
  56. int putfullfstr( Point where, char *prompt, char *fstr ) {
  57.     SFReply reply;
  58.     int err;
  59.  
  60.     printf("\n"); // 2
  61.     SFPutFile( where, CtoPstr(prompt) , CtoPstr(fstr), NULL, &reply);
  62.  
  63.     if (!reply.good) return 1; // 3
  64.  
  65.     err = PathNameFromWD( reply.vRefNum, fstr ); // 4
  66.     if (err) return 1;
  67.     strcat( fstr, PtoCstr( reply.fName ) ); // 5
  68.     printf( "Output File Chosen:\n%s\n", fstr );
  69.     return 0;
  70. }
  71. /*
  72. 1. The appropriate dialog box is placed at screen location "where".
  73.    The full name of the file chosen is returned in "fstr".
  74.    The function returns 1 on error and zero on no error.
  75.    It is strongly recommended that "fstr" be 256 bytes long, 
  76.    though one could usually get away with a length of about 80.
  77. 2. For reasons unclear to me, it is necessary to write to the console for
  78.    SF???File() to work.  An error sends me into MacsBug otherwise.
  79. 3. User clicking "Cancel" is considered an error.
  80. 4. Put full path name of directory into fstr.
  81. 5. Append file name to full path name to yield full file name.
  82. */
  83.  
  84. /*
  85. PathNameFromWD() puts the full pathname of the HFS working directory
  86. "vRefNum" into "pathName".
  87. It does this by calling PBGetWDInfo to get the VRefNum and DirID of the
  88. real directory. It then calls PathNameFromDirID, and returns its result.
  89. */
  90. int PathNameFromWD(long vRefNum, char* pathName)
  91. {
  92.     WDPBRec myBlock;
  93.     OSErr err;
  94.  
  95.     myBlock.ioNamePtr  = nil;     // 1
  96.     myBlock.ioVRefNum  = vRefNum; // 1
  97.     myBlock.ioWDIndex  = 0;       // 1
  98.     myBlock.ioWDProcID = 0;       // 1
  99.     
  100.     err = PBGetWDInfo(&myBlock, FALSE); // 2
  101.     if (err) return 1;
  102.  
  103.     return PathNameFromDirID(myBlock.ioWDDirID, myBlock.ioWDVRefNum,
  104.                                                                 pathName);
  105. }
  106. /*
  107. 1. Prepare parameters to be passed in call to PBGetWDInfo()
  108. 2. Change the Working Directory number in vRefnum into a real
  109.    vRefnum and DirID. The real vRefnum is returned in ioVRefnum,
  110.    and the real DirID is returned in ioWDDirID.
  111. */
  112.  
  113.  
  114. /*
  115. PathNameFromDirID() is a utility function used by PathNameFromWD()
  116. */
  117. int PathNameFromDirID(long dirID, short vRefNum, char* fullPathName)
  118. {
  119.     DirInfo block;
  120.     Str255 directoryName;
  121.     OSErr err;
  122.  
  123.     *fullPathName = '\0'; // 1
  124.  
  125.     block.ioDrParID = dirID; // 2
  126.     block.ioNamePtr = directoryName; // 3
  127.     do {
  128.         block.ioVRefNum   = vRefNum; // 4
  129.         block.ioFDirIndex = -1; // 4
  130.         block.ioDrDirID   = block.ioDrParID; // 4,5
  131.  
  132.         err = PBGetCatInfo(&block, FALSE);  // 6
  133.         if (err) return 1;
  134.  
  135.         directoryName[++directoryName[0]] = ':'; // 7 
  136.  
  137.         PtoCstr(directoryName); // 8
  138.         strcat( (char *) directoryName, fullPathName ); // 9
  139.         strcpy( fullPathName, (char *) directoryName ); // 10
  140.     } while (block.ioDrDirID != 2); // 11
  141.     return 0;
  142. }
  143. /*
  144.  1. Initialize to the null string
  145.  2. Parent directory = most local directory.  This is an initialization
  146.      needed before loop entry.  It makes sense if you consider the most
  147.      local directory the parent directory of the file in question.
  148.  3. Put directory names in "directoryName"
  149.  4. Prepare parameters to be passed in call to PBGetCatInfo();
  150.  5. Set the block's DrDirID to that of its parent.  This is how we travel up
  151.     the directory tree.
  152.  6. Get catalog information for this block.  In our case, the only info we
  153.      use is the string at ioNamePtr (directoryName) and ioDrParID.
  154.  7. Append ':' onto the new directory name & increment string length byte
  155.     directoryName will be of the form "DirName:"
  156.  8. Convert Pascal --> C string format
  157.  9. Append the lower part of the path onto the new directory name
  158.     fullPathName will be of the form "DirName2:DirName1:DirName0:"
  159. 10. Make a copy of directoryName in fullPathName since directoryName will 
  160.     be overwritten on next pass through PBGetCatInfo
  161. 11. Stop when the highest level directory (the volume) has been reached
  162. */